home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / macabuse / src / net / inc / ipx.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  12.6 KB  |  370 lines

  1. #ifndef __IPX_SOCK_HPP_
  2. #define __IPX_SOCK_HPP_
  3.  
  4. #include "sock.hpp"
  5. #include "macs.hpp"
  6. #include "timing.hpp"
  7. #include <string.h>
  8.  
  9. #include <string.h>
  10.  
  11. class ipx_net_address : public net_address
  12.   public :
  13.   uchar addr[10];
  14.   ushort port;      // stored in intel-format
  15.   virtual protocol protocol_type() { return IPX; }
  16.   virtual int equal(net_address *who)   { return who->protocol_type()==protocol_type() && 
  17.                              memcmp(addr,((ipx_net_address *)who)->addr,10)==0; }
  18.   virtual int set_port(int Port);
  19.   virtual void print() { printf("network = %x.%x.%x.%x, node = %x.%x.%x.%x.%x.%x, port = %d\n",
  20.                          addr[0],addr[1],addr[2],addr[3],addr[4],addr[5],addr[6],addr[7],addr[8],addr[9],port); }  
  21.   ipx_net_address() { memset(addr,0,10); port=0; }
  22.   ipx_net_address(uchar *addr_data, ushort port) : port(port) 
  23.   { memcpy(addr,addr_data,10); }
  24.   int get_port() { return port ; }
  25.   net_address *copy() { return new ipx_net_address(addr,port); }
  26.   void store_string(char *st, int st_length)
  27.   {
  28.     char buf[100];
  29.     sprintf(buf,"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x:%d",addr[0],addr[1],addr[2],addr[3],addr[4],addr[5],addr[6],addr[7],addr[8],addr[9],port);
  30.     strncpy(st,buf,st_length);
  31.     st[st_length-1]=0;
  32.   }
  33. } ;
  34.  
  35. #pragma pack (1)
  36.  
  37.  
  38. class ipx_net_socket : public net_socket
  39. {
  40.   public :
  41.   uchar *backup;                   // when a partial read request happens
  42.   int backup_start,backup_end,backup_size;
  43.   int read_backup(void *&buf, int &size);
  44.   void add_backup(uchar *buf, int size);
  45.  
  46.   ipx_net_socket *next;
  47.   int total_packets;
  48.  
  49.   int ipx_packet_total() { return total_packets; }
  50.  
  51.   enum {IPX_PACKET_SIZE=1024} ;      // this amount of data space reserved for each packet
  52.                                     // though the actaul packet sent may be smaller because of IPX limits
  53.  
  54.  
  55.   enum { ACK=1,            // acknowedge receipt of packet
  56.      FIN=2,            // connection has finished
  57.      CONNECT=4,        // asking to make a connection
  58.      WRITE_DATA=8,
  59.      WRITE_CONFIRM=16,
  60.      ALL_FLAGS=31 };
  61.  
  62.   enum { SEND_RETRY_TIMEOUT=10,  // .1 seconds timeout before resending data packet
  63.      RETRY_TOTAL=2400        // give up after retring 2400 times, or 240 seconds =  4 minutes
  64.        } ;
  65.  
  66.  
  67.  
  68.   struct ipx_packet
  69.   {
  70.     struct ECBStructure
  71.     {
  72.       ushort Link[2];
  73.       ushort ESRAddress[2];
  74.       uchar  InUseFlag;
  75.       uchar  CompletionCode;
  76.       ushort ECBSocket;
  77.       uchar  IPXWorkspace[4];
  78.       uchar  DriverWorkspace[12]; 
  79.       uchar  ImmediateAddress[6]; 
  80.       ushort FragmentCount;
  81.       ushort fAddress[2];
  82.       ushort fSize;
  83.  
  84.       int data_waiting() { return InUseFlag; }
  85.       void set_data_size(int size);
  86.     } ecb;
  87.  
  88.     struct IPXPacketStructure
  89.     {
  90.       ushort PacketCheckSum;
  91.       ushort PacketLength;
  92.       uchar  PacketTransportControl;
  93.       uchar  PacketType;
  94.  
  95.       uchar  dAddr[10];
  96.       ushort dSocket;      // flipped intel-format
  97.  
  98.       uchar  sAddr[10];
  99.       ushort sSocket;     // flipped intel-format
  100.  
  101.     } ipx;
  102.  
  103.  
  104.     uchar data[IPX_PACKET_SIZE];
  105.  
  106.     int packet_prefix_size()                 { return 6; }    // 2 size
  107.                                                               // 2 byte check sum
  108.                                                               // 1 byte packet order
  109.                                                               // 1 byte flags
  110.        
  111.  
  112.     void set_packet_size(unsigned short x)   { *((unsigned short *)data)=lstl(x); }
  113.     unsigned short packet_size()             { unsigned short size=(*(unsigned short *)data); return lstl(size); }
  114.     unsigned char tick_received()            { return data[4]; }  
  115.     void set_tick_received(unsigned char x)  { data[4]=x; }
  116.     unsigned char *packet_data()             { return data+packet_prefix_size(); }
  117.     unsigned short get_checksum()            { unsigned short cs=*((unsigned short *)data+1); return lstl(cs); }
  118.     uchar get_flag(int flag)                 { return data[5]&flag; }
  119.     void set_flag(int flag, int x)           { if (x) data[5]|=flag; else data[5]&=~flag; }
  120.     unsigned short calc_checksum()
  121.     {
  122.       *((unsigned short *)data+1)=0;
  123.       int i,size=packet_prefix_size()+packet_size();
  124.       unsigned char c1=0x12,c2=0x34,*p=data;
  125.       for (i=0;i<size;i++,p++)
  126.       {
  127.     c1+=*p;
  128.     c2+=c1;
  129.       }
  130.       unsigned short cs=( (((unsigned short)c1)<<8) | c2);
  131.       *((unsigned short *)data+1)=lstl(cs);
  132.       return cs;
  133.     }
  134.  
  135.  
  136.     void reset()    { set_packet_size(0); set_flag(ALL_FLAGS,0); }     // 2 bytes for size, 1 byte for tick
  137.     
  138.     void add_to_packet(void *buf, int size);
  139.  
  140.     void write_byte(unsigned char x) { add_to_packet(&x,1); }
  141.     void write_short(unsigned short x) { x=lstl(x); add_to_packet(&x,2); }
  142.     void write_long(unsigned long x) { x=lltl(x); add_to_packet(&x,4); }
  143.  
  144.  
  145.   } *pk;
  146.  
  147.      
  148.  
  149.   int fd,er;
  150.   void idle();   // call this if you are bored
  151.   
  152.   enum { INIT=0,
  153.          READ_CHECKING=1,
  154.      WRITE_CHECKING=2,
  155.      READ_SELECTED=4,
  156.      WRITE_SELECTED=8 
  157.        } ;
  158.   uchar status;
  159.  
  160.   void send_data(int data_size);
  161.   void send_ping_packet();
  162.   int listen_to_packet(int packet_num);
  163.   int open_socket(int port);
  164.  
  165.   virtual int error()                                              { return er; }
  166.   virtual int ready_to_read();
  167.   virtual int ready_to_write() { if (fd>=0) return pk[0].ecb.InUseFlag; else return 1; }
  168.  
  169.   virtual int write(void *buf, int size, net_address *addr=NULL) { return 0; }
  170.  
  171.   virtual int read(void *buf, int size, net_address **addr=0) { if (addr) *addr=0; return 0; }
  172.   virtual int get_fd()    { return fd; }
  173.   virtual void read_selectable()     { status|=READ_CHECKING; }
  174.   virtual void read_unselectable()   { status&=~READ_CHECKING; }
  175.   virtual void write_selectable()    { status|=WRITE_CHECKING; }
  176.   virtual void write_unselectable()  { status&=~WRITE_CHECKING; }
  177.   virtual int listen(int port)       { return 0; }
  178.   virtual net_socket *accept(net_address *&from) { from=NULL; return NULL; }
  179.   virtual int from_port() { return -1; }
  180.  
  181.   virtual void clear();
  182.  
  183.   void set_socket_dest(uchar *addr, ushort port);
  184.   ipx_net_socket(int port, int total_read_packets, ipx_net_address *dest);
  185.   virtual void print() { fprintf(stderr,"ipx net socket");  }
  186.   virtual ~ipx_net_socket();
  187. } ;
  188.  
  189. #pragma pack (0)
  190.  
  191.  
  192. class ipx_secure_listening_socket;
  193.  
  194. class ipx_secure_socket : public ipx_net_socket
  195. {  
  196.   uchar packet_on,write_ack_needed,write_ack_checksum;
  197.   ushort parent_port;
  198.  
  199.   enum { ESTABLISHED, 
  200.      CLOSE_WAIT,        // waiting for user to close
  201.      CLOSING,           // waiting for remote response to close
  202.      TIME_WAIT,         // waiting till 'counter' expires to ensure remote socket has gotten close message
  203.        } state;
  204.  
  205.   int counter,closed;
  206.   
  207.   public :
  208.   enum { SECURE_TOTAL_READ_PACKETS=2 } ;
  209.  
  210.   virtual int ready_to_read();
  211.   virtual int ready_to_write() { return 1; }
  212.  
  213.   virtual int write(void *buf, int size, net_address *addr=NULL);
  214.   virtual int read(void *buf, int size, net_address **addr=0);
  215.  
  216.   void clear_packet(int i);
  217.   ipx_secure_socket(int parent_port, int port, ipx_net_address *dest) : 
  218.     ipx_net_socket(port,SECURE_TOTAL_READ_PACKETS,dest),parent_port(parent_port)
  219.   { packet_on=1; write_ack_needed=0; closed=0; }
  220.   int from_port() { return parent_port; }
  221.   int verify_sync();
  222.   virtual void print() { fprintf(stderr,"ipx secure socket");  }
  223.   ~ipx_secure_socket();
  224. } ;
  225.  
  226.  
  227. // the closed_secure_ipx_packet is not to be seen by anyone other than the ipx_protocol
  228. // and it's is polled during calls to select()
  229.  
  230. class closed_secure_ipx_socket
  231. {
  232.   public :
  233.   closed_secure_ipx_socket *next;
  234.  
  235.   enum { CLOSING,                       // keep send close message, until receive ack, then goto TIME_WAIT
  236.      TIME_WAIT                      // after ack for close message, wait for TIME_WAIT_EXPIRE before closing
  237.        } state;
  238.  
  239.   enum { CLOSE_MSG_RETRY_TIMEOUT=20,    // .2 seconds between each close() message retry
  240.          CLOSE_TOTAL_RETRY=10,          // 10 total retries for sending a close() message before giving up
  241.      TIME_WAIT_EXPIRE=1000          // 10 seconds before a socket expires and can be reused
  242.        } ;
  243.  
  244.   time_marker start_counter;      // if CLOSING, then the clock() of the last close message sent
  245.                                   // if TIME_WAIT, then clock() of the entering this state
  246.  
  247.   int close_send_count;           // number of times close socket msg has been sent (from CLOSING state)
  248.                                   
  249.   int fd;                          // the port this socket is using
  250.   uchar final_packet_number;
  251.   ipx_net_socket::ipx_packet *pk;  // pointer to low packet memory taken from a ipx_secure_socket
  252.   int poll();                      // called by select, return 0 when socket is expired
  253.   ~closed_secure_ipx_socket();
  254.   int tp;
  255.   closed_secure_ipx_socket(ipx_net_socket::ipx_packet *pk, 
  256.                int total_packets,
  257.                int fd, 
  258.                unsigned char final_packet_number, 
  259.                closed_secure_ipx_socket *next);
  260.  
  261.   void print() { fprintf(stderr,"closed secure socket, state=%d",(int)state);  }
  262. } ;
  263.  
  264.  
  265. // this class can only sit and listen, it will respond to 'ping' and 'connect' packets
  266. // this class will return 1 for ready_to_read and then create a ipx_secure_socket on accept()
  267.  
  268. class ipx_secure_listening_socket : public ipx_net_socket
  269. {
  270.   enum { NAME_MAX=30 };
  271.   char name[NAME_MAX];
  272.   public :
  273.  
  274.   virtual int ready_to_write() { return 1; }
  275.   virtual int ready_to_read();
  276.   virtual int write(void *buf, int size, net_address *addr=NULL) { return 0; }
  277.   virtual int read(void *buf, int size, net_address **addr=0) { if (addr) *addr=0; return 0; }
  278.  
  279.   virtual net_socket *accept(net_address *&from);
  280.   ipx_secure_listening_socket(int port, ipx_net_address *dest, char *sock_name) : ipx_net_socket(port,2,dest) 
  281.   { strncpy(name,sock_name,NAME_MAX-1); name[NAME_MAX-1]=0; }
  282.   ~ipx_secure_listening_socket();
  283.   virtual void print() { fprintf(stderr,"ipx secure listening socket");  }
  284. } ;
  285.  
  286. class ipx_fast_socket : public ipx_net_socket
  287. {
  288.   public :
  289.   int write(void *buf, int size, net_address *addr=NULL);
  290.   int read(void *buf, int size, net_address **addr=0);
  291.   virtual int ready_to_read();
  292.   ipx_fast_socket(int port, ipx_net_address *dest) : ipx_net_socket(port,16,dest) { ; }
  293.   void clear() { ; }  // no need to clear these
  294.   virtual void print() { fprintf(stderr,"ipx fast socket");  }
  295.  
  296. } ;
  297.  
  298. class ipx_net_protocol : public net_protocol
  299. {
  300.   int Installed;
  301.   ushort max_pksz;
  302.   int max_packet_size() { return max_pksz; }
  303.  
  304.   net_address *find_host(int port, int timeout);
  305.   ipx_net_socket *list;
  306.  
  307.   class locator_node
  308.   {
  309.     public :
  310.     ipx_net_address *addr;
  311.     locator_node *next;
  312.     locator_node(ipx_net_address *addr, locator_node *next) : addr(addr), next(next) { ; }
  313.     ~locator_node() { delete addr; }
  314.   } *find_list;
  315.  
  316.   ipx_net_socket *locator_socket;
  317.   time_marker last_ping;
  318.   enum { LOCATOR_PACKETS=16 } ;
  319.  
  320.   public :
  321.   void *low_paragraph;
  322.  
  323.   enum { CONNECT_PING_RETRY=10,     // retry connetc command every .1 seconds
  324.      CONNECT_RETRY_TOTAL=2400  // retry for 4 mins.
  325.        } ;
  326.      
  327.   closed_secure_ipx_socket *closed_list;
  328.  
  329.   uchar local_addr[10];
  330.  
  331.   virtual net_address *get_local_address();
  332.   virtual net_address *get_node_address(char *&server_name, int def_port, int force_port);
  333.   virtual net_socket *connect_to_server(net_address *addr, 
  334.                     net_socket::socket_type sock_type=net_socket::SOCKET_SECURE
  335.                     );  
  336.   virtual net_socket *create_listen_socket(int port, net_socket::socket_type sock_type, char *sock_name);
  337.  
  338.   int select(int block);
  339.  
  340.   int installed();
  341.  
  342.   char *name()    { return "IPX driver"; }
  343.   void add_socket_to_list(ipx_net_socket *who);
  344.   void remove_socket_from_list(ipx_net_socket *who);
  345.  
  346.   int max_packet_data_size() { int x=max_pksz-sizeof(ipx_net_socket::ipx_packet::IPXPacketStructure);  // max IPX will allow
  347.                    if (x>ipx_net_socket::IPX_PACKET_SIZE)                      // max space we have
  348.                       return ipx_net_socket::IPX_PACKET_SIZE; else return x; 
  349.                  }
  350.   void clear_sockets(ipx_net_socket *exclude);
  351.   ipx_net_socket *socket_origination(uchar *addr, ushort other_port);     // finds a socket connected to address, started from port 'port'
  352.   void add_closed(ipx_net_socket::ipx_packet *pk, int total_packets, int fd, int packet_on);
  353.   void cleanup();
  354.   int free_closed_socket();       // returns 1 if able to close 1 closed socket in FIN state
  355.   int socket_can_be_allocated(int num_packets);
  356.   ipx_net_protocol();
  357.   void show_socks();
  358.   ~ipx_net_protocol();
  359.  
  360.   net_address *find_address(int port, char *name);
  361.   void reset_find_list();
  362. } ;
  363.  
  364. extern ipx_net_protocol ipx_net;
  365.  
  366.  
  367. #endif
  368.  
  369.